<?php

// A set of functions for control

class CF_Controls{


// This array defines special psudo_attributes which work on form elements.  If one of them is defined in the settings array, 
// then a function is called with the name defined in this array for that key.  The  argument to the array is the type of control
protected static $ar_att_compute = array('set_name'=>'set_name', 'set_on_change'=>'set_on_change');




function AttributesFromArray($settings,$type){
	$ar_att_compute = array('set_name'=>'set_name',
	 'set_on_change'=>'set_on_change');
	if(is_array($settings)){
		foreach($settings as $setting=>$value){
			if($ar_att_compute[$setting] == null){
				$html .= $setting . '="' . $value . '" ';
			}
			else{
				// here, get special settings

				$html = self::$setting($value,$type);
			}
		}
	}
	else{
		$html .= $settings;
	}
	return $html;
}

function set_on_change($value,$type){
	print 'Value is ' . $value . ' And Type is ' . $type;
}

function text($settings){
	$type = 'text';
	$html = "<input type=\"text\" ";
	$html .= self::AttributesFromArray($settings,$type);
	
	$html .= " />\n";
	return $html;
}

function drop_down($settings,$list,$selection){
	print_r($list);
	$type = 'select';
	if(is_array($selection)){
		foreach($selection as $sel){
			$ar_selections[$sel] = true;
		}
	}else{
		$ar_selections[$selection] = true;
	}
	
	$html = "<select  ";
		if(is_array($settings)){
		foreach($settings as $setting=>$value){
			if($ar_att_compute[$setting] == null){
				$html .= $setting . '="' . $value . '" ';
			}

		}
	}
	else{
		$html .= $settings;
	}
	$html .= " /> \n";
	foreach($list as $listitem => $item){

		if(is_array($item)){

			$html .= "<OPTION ";
			if ($ar_selections[(string)$item[$keyvalue]] != null) {
				$html .= "selected=\"selected\" " ;
				}
			$html .= "value=\"";
			$html .= $item['keyvalue'];
			$html .="\">";
			//$html .= substr($item[$showvalue],0,$textlimit);
			$html .= $item['showvalue'];
			$html .="</OPTION>\n";

		}
		else{
			$html .= "<OPTION ";
			if ($ar_selections[(string)$item[$keyvalue]] != null) {
				$html .= "selected=\"selected\" " ;
				}
			$html .= "value=\"";
			$html .= $listitem;
			$html .="\">";
			//$html .= substr($item[$showvalue],0,$textlimit);
			$html .= $item;
			$html .="</OPTION>\n";	
		}
	}
	$html .= "</select>\n";
	return $html;
	
}

function date($settings){
	$type = 'text';
	$html = "<input type=\"text\" ";
	($settings['jq_image_dir'] == null)?$imagedir = 'images/':$imagedir=$settings['jq_image_dir'];
	($settings['jq_dir'] == null)?$jqdir = 'jq/':$jqdir=$settings['jq_dir'];	
	if(is_array($settings)){
		foreach($settings as $setting=>$value){
			if($ar_att_compute[$setting] == null){
				$html .= $setting . '="' . $value . '" ';
			}

		}
	}
	else{
		$html .= $settings;
	}
	
	$html .= " />\n";
	return $html;
}

}


?>
